/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MiniProgJPanel.java
 *
 * Created on 2011-6-30, 11:40:57
 */
package seso;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.GradientPaint;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.jdesktop.application.Action;
import java.awt.Image;

/**
 *
 * @author Administrator
 */
public class RMiniProgJPanel extends javax.swing.JPanel {

    public int indexMiniProg = -1;
    private boolean mouse_Entered = false, total_MouseEntered = false;
    private final int arcWH = 8;
    private String progID = "PDS050";
    //private String ProgName = "XM OK Feature Option RV Customer Order Europe Order ABCZ GOOD";
    private String ProgName = "XM OK Feature";
    private boolean isFirstTime = true;
    RMiddleProgJPanel rMiddleProgJPanel;
    javax.swing.JPanel homePageBottomJPanel;
    Image dialogImage = null;
    
    public javax.swing.JDialog dlg = null;
    int originalX = 0, originalY = 0;

    /** Creates new form MiniProgJPanel */
    public RMiniProgJPanel() {
          initComponents();
    }

    public RMiniProgJPanel(javax.swing.JDialog dlg, int originalX, int originalY, int indexMiniProg, String progID, String ProgDescription, Image dialogImage) {
        this.dlg = dlg;
        this.originalX = originalX;
        this.originalY = originalY;
        this.indexMiniProg = indexMiniProg;
        this.progID = progID;
        this.ProgName = ProgDescription;
        this.dialogImage = dialogImage;

        initComponents();
        addListener();
        
        MoreConstructClass();
    }

    
    public void replaceRMiniProgJPanel(javax.swing.JDialog dlg, int originalX, int originalY, int indexMiniProg, String progID, String ProgDescription, Image dialogImage) {
        this.dlg = dlg;
        this.originalX = originalX;
        this.originalY = originalY;
        this.indexMiniProg = indexMiniProg;
        this.progID = progID;
        this.ProgName = ProgDescription;
        this.dialogImage = dialogImage;

        MoreConstructClass();
    }
    
    
    
    private void MoreConstructClass() {
        setControl();
    }

    private void setControl() {
        progTitleJLabel.setText(progID + "-" + ProgName.replace(" ", ""));
        
        miniProgRImageBox.setImage(dialogImage);
        rMiddleProgJPanel = new RMiddleProgJPanel(ProgName + " - " + progID, dialogImage);
    }
    
    public void updateLocationAndImage(int originalX, int originalY, Image dialogImage){
        this.originalX = originalX;
        this.originalY = originalY;
        this.dialogImage = dialogImage;
        miniProgRImageBox.setImage(dialogImage);
        rMiddleProgJPanel.updateImage(dialogImage);
        repaint();
    }
    
    
    private void addListener() {
        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouse_Entered = true;
                mouseEnteredAction();
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouse_Entered = false;
                mouseExitedAction();
            }
        });
    }

    public void mouseEnteredAction() {
        if (homePageBottomJPanel == null) {
            return;
        }

        total_MouseEntered = (mouse_Entered || rMiniProgCloseJButton.isMouseEntered());

        if (homePageBottomJPanel != null) {
            if (isFirstTime) {
                homePageBottomJPanel.add(rMiddleProgJPanel);
            }
            rMiddleProgJPanel.setBounds(getX(), 0, rMiddleProgJPanel.getPreferredSize().width, rMiddleProgJPanel.getPreferredSize().height);
            rMiddleProgJPanel.setVisible(true);
            isFirstTime = false;
        }
        repaint();
    }

    public void mouseExitedAction() {
        if (homePageBottomJPanel == null) {
            return;
        }

        total_MouseEntered = (mouse_Entered || rMiniProgCloseJButton.isMouseEntered());

        if (!total_MouseEntered) {
            rMiddleProgJPanel.setVisible(false);
            homePageBottomJPanel.removeAll();
            isFirstTime = true;
        }
        repaint();
    }

    public void setHomePageBottomJPanel(javax.swing.JPanel homePageBottomJPanel) {
        this.homePageBottomJPanel = homePageBottomJPanel;
    }

    @Override
    protected void paintComponent(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        int width = getWidth();
        int height = getHeight();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, width, height, arcWH, arcWH);
        g2d.setClip(rect);

        if (total_MouseEntered) {
            progTitleJLabel.setText(ProgName.replace(" ", ""));
        } else {
            progTitleJLabel.setText(progID + "-" + ProgName.replace(" ", ""));
        }

        super.paintComponent(g);
    }

    @Override
    protected void paintBorder(java.awt.Graphics g) {
        RoundRectangle2D.Double rect;
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();

        if (total_MouseEntered) {
            g2d.setColor(new Color(255, 255, 255));
            rect = new RoundRectangle2D.Double(1, 1, width - 2, height - 2, arcWH, arcWH);
            g2d.draw(rect);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rMiniProgCloseJButton = new seso.RMiniProgCloseJButton();
        progTitleJLabel = new javax.swing.JLabel();
        miniProgRImageBox = new seso.RImageBox();

        setFocusable(false);
        setName("Form"); // NOI18N
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(109, 68));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        setLayout(null);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(seso.SesoApp.class).getContext().getActionMap(RMiniProgJPanel.class, this);
        rMiniProgCloseJButton.setAction(actionMap.get("rMiniProgCloseJButtonAction")); // NOI18N
        rMiniProgCloseJButton.setBorder(null);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(seso.SesoApp.class).getContext().getResourceMap(RMiniProgJPanel.class);
        rMiniProgCloseJButton.setIcon(resourceMap.getIcon("rMiniProgCloseJButton.icon")); // NOI18N
        rMiniProgCloseJButton.setText(resourceMap.getString("rMiniProgCloseJButton.text")); // NOI18N
        rMiniProgCloseJButton.setBorderPainted(false);
        rMiniProgCloseJButton.setContentAreaFilled(false);
        rMiniProgCloseJButton.setFocusPainted(false);
        rMiniProgCloseJButton.setFocusable(false);
        rMiniProgCloseJButton.setName("rMiniProgCloseJButton"); // NOI18N
        rMiniProgCloseJButton.setRolloverIcon(resourceMap.getIcon("rMiniProgCloseJButton.rolloverIcon")); // NOI18N
        rMiniProgCloseJButton.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        rMiniProgCloseJButton.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        add(rMiniProgCloseJButton);
        rMiniProgCloseJButton.setBounds(79, 8, 17, 15);

        progTitleJLabel.setFont(resourceMap.getFont("progTitleJLabel.font")); // NOI18N
        progTitleJLabel.setForeground(resourceMap.getColor("progTitleJLabel.foreground")); // NOI18N
        progTitleJLabel.setText(resourceMap.getString("progTitleJLabel.text")); // NOI18N
        progTitleJLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        progTitleJLabel.setName("progTitleJLabel"); // NOI18N
        progTitleJLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        add(progTitleJLabel);
        progTitleJLabel.setBounds(11, 49, 96, 20);

        miniProgRImageBox.setText(resourceMap.getString("miniProgRImageBox.text")); // NOI18N
        miniProgRImageBox.setName("miniProgRImageBox"); // NOI18N
        add(miniProgRImageBox);
        miniProgRImageBox.setBounds(11, 6, 60, 40);
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked

        // 显示最大化图标的变化过程
        RMiniImageJDialog rdlg = new RMiniImageJDialog(null, true);
        //rdlg.setImage((java.awt.Image) dialogImage, originalX, originalY, originalWidth, originalHeight, goalX, goalY, false);
        rdlg.setImage((java.awt.Image) dialogImage, getX(), getY(), dialogImage.getWidth(null), dialogImage.getHeight(null), originalX, originalY, false);
        rdlg.setVisible(false);
        SesoApp.getApplication().show(rdlg);
        // 显示最大化图标的变化过程
        
        dlg.setLocation(originalX, originalY);
        dlg.setVisible(true);
    }//GEN-LAST:event_formMouseClicked

    @Action
    public void rMiniProgCloseJButtonAction() {
        
        dlg.dispose();

        homePageBottomJPanel.removeAll();
        homePageBottomJPanel.repaint();

        JPanel miniProgListJPanel = (JPanel) (this.getParent());
        for (int i = 0; i < miniProgListJPanel.getComponentCount(); i++) {
            if ((miniProgListJPanel.getComponent(i).getName()).equals("rMX" + indexMiniProg)) {
                miniProgListJPanel.remove(i);
                break;
            }
        }

        int counterMiniProg = miniProgListJPanel.getComponentCount();

        miniProgListJPanel.setSize(counterMiniProg * (getPreferredSize().width + 5), miniProgListJPanel.getHeight());

        
        if (miniProgListJPanel.getComponentCount() > 0) {
            miniProgListJPanel.getComponent(0).setVisible(false);
            miniProgListJPanel.repaint();
            miniProgListJPanel.getComponent(0).setVisible(true);
            miniProgListJPanel.repaint();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private seso.RImageBox miniProgRImageBox;
    private javax.swing.JLabel progTitleJLabel;
    private seso.RMiniProgCloseJButton rMiniProgCloseJButton;
    // End of variables declaration//GEN-END:variables
}
